package com.caseProcess.controller;

import com.alibaba.fastjson.JSONArray;
import com.caseProcess.entity.*;
import com.caseProcess.service.UserService;
import com.caseProcess.service.VUserInfoService;
import com.caseProcess.utils.PagingUtil;
import com.caseProcess.utils.TimeUtil;
import com.caseProcess.utils.util;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private VUserInfoService vUserInfoService;

    //批量导入用户
    @RequestMapping("/userImport")
    public Result userImport(String jsonArrayUser) {

        Result result = new Result();
        try {
            List<User> userList= JSONArray.parseArray(jsonArrayUser,User.class);
            int res = userService.batchInsert(userList);
            if (res > 0) {
                result.setCode(Result.RESULT_OK);
                result.setMsg("操作成功");
            } else {
                result.setCode(Result.RESULT_ERROR);
                result.setMsg("操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败，主键冲突，请先修改重复警情编号");
            return result;
        }
    }

    /**
     * 添加用户
     * @return
     */
    @CrossOrigin
    @RequestMapping("/insert")
    public Result insert(String policeId,String fullName,Boolean agender,String phone,Long roleId,String departmentId) throws NoSuchAlgorithmException {
        Result result=new Result();
        User u=userService.isExist(policeId);
       if (u!=null){
           result.setCode(result.RESULT_ERROR);
           result.setMsg("失败");
           return result;
       }else{
           User user=new User();
           user.setPoliceId(policeId);
           user.setFullName(fullName);
           if(agender==null){
               user.setAgender(true);
           }else {
               user.setAgender(agender);
           }
           user.setPhone(phone);
           user.setPwd(util.UseMD5("123456"));
           Role role=new Role();
           role.setRoleId(roleId);
           user.setRole(role);

           Department department=new Department();
           department.setDepartmentId(departmentId);
           user.setDepartment(department);

           userService.insert(user);
           result.setMsg("成功");
           result.setData(user);
           return result;
       }
    }

    /**
     * 删除某个用户
     * @param policeId
     * @return
     */
    @CrossOrigin
    @RequestMapping("/delete")
    public Result delete(@RequestParam String  policeId){
        int a=userService.delete(policeId);
        Result result=new Result();
        result.setCode(Result.RESULT_OK);
        result.setMsg("用户信息删除成功");
        result.setData(a);
        return result;
    }


    /**
     * 修改用户信息
     * 个人信息修改
     */
    @CrossOrigin
    @RequestMapping("/updateInfo")
    public Result updateInfo(@RequestParam(required = false,defaultValue = "0")int noChangeSession,String policeId, String fullName, Boolean agender, String phone, Long roleId, String departmentId, HttpServletRequest request){
        Result result=new Result();
        User user=new User();
        user.setPoliceId(policeId);
        user.setFullName(fullName);
        user.setPhone(phone);
        user.setAgender(agender);
        Department department=new Department();
        department.setDepartmentId(departmentId);
        user.setDepartment(department);

        Role role=new Role();
        role.setRoleId(roleId);
        user.setRole(role);

        userService.update(user);
        VUserInfo vUserInfo=new VUserInfo();
        vUserInfo.setPoliceId(policeId);
        List<VUserInfo> list=vUserInfoService.selectVUserInfosBy(vUserInfo);
        if(list.isEmpty()){
            result.setMsg("用户信息修改异常");
            result.setCode(Result.RESULT_ERROR);
            return result;
        }
        if(noChangeSession==0) {
            request.getSession().setAttribute("police", list.get(0));
        }
        result.setCode(Result.RESULT_OK);
        result.setMsg("用户信息修改成功");
        result.setData(list.get(0));
        return result;

    }

    /**
    *查询所有用户信息列表
    */
    @CrossOrigin
    @RequestMapping("/selectAll")
    public Result selectAll( @RequestParam(required = false,defaultValue = "0")int page,
                             @RequestParam(required = false,defaultValue = "0")int limit){
        List<User> users=userService.select();
        Result result = new Result();
        result.setCount(users.size());
        if(page!=0&&limit!=0){
            users= PagingUtil.paging(users,page,limit);
        }
        result.setData(users);
        result.setCode(Result.RESULT_OK);
        result.setMsg("用户信息查询成功");
        return result;

    }

    /**
     * 根据条件查询用户
     * @param policeId 用户id
     * @param policeName 用户姓名
     * @param departmentId 部门id
     * @param page
     * @param limit
     * @return
     */
    @CrossOrigin
    @RequestMapping("/selectAllBy")
    public Result selectAllBy(String policeId,String policeName,String departmentId,int ctState, @RequestParam(required = false,defaultValue = "0")int page,
                              @RequestParam(required = false,defaultValue = "0")int limit){
        User user=new User();
        user.setPoliceId(policeId);
        user.setFullName(policeName);
        Department department=new Department();
        department.setDepartmentId(departmentId);
        user.setDepartment(department);
        user.setIsLocking(ctState);
        List<User> users=userService.selectAllBy(user);
        Result result = new Result();
        result.setCount(users.size());
        if(page!=0&&limit!=0){
            users= PagingUtil.paging(users,page,limit);
        }
        result.setData(users);
        result.setMsg("用户信息查询成功");
        return result;

    }

    /**
     * 获取用户
     * @param roleId 角色Id
     * @return 操作结果及数据
     */
    @RequestMapping("/getUser")
    public Result getUser(String roleId) {
        List<User> users = userService.getUser(roleId);
        Result result = new Result();
        if (users.size() >= 0) {
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(users);
        } else {
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /*密码重置*/
    @CrossOrigin
    @RequestMapping("/updatePwd")
    public Result updatePwdByA(@RequestParam String policeId) {
        User user=userService.getUserpwd(policeId);
        try {
            user.setPwd(util.UseMD5(util.getFirstSpell("123456")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        userService.updatePwd(user);
        Result result=new Result();
        result.setCode(Result.RESULT_OK);
        result.setMsg("密码重置成功");
        result.setData(user);
        return result;
    }

    /*用户密码修改*/
    @CrossOrigin
    @RequestMapping("/updatePwdByP")
    public Result updatePwduser(@RequestParam String pwd,String oldPwd,String policeId) {
        User user=new User();
        String str = null;
        try {
         user.setPwd(util.UseMD5(util.getFirstSpell(oldPwd)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        user=userService.getUserandPwd(policeId,user.getPwd());

        if(user==null){
            Result result=new Result();
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("密码修改失败");
            result.setData(user);
            return result;
        }else{
            try {
                user.setPwd(util.UseMD5(util.getFirstSpell(pwd)));
            } catch (Exception e) {
                e.printStackTrace();
            }
            userService.updatePwd(user);
            Result result=new Result();
            result.setCode(Result.RESULT_OK);
            result.setMsg("密码修改成功");
            result.setData(user);
            return result;
        }
    }
}
